using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MonsterUI : MonoBehaviour
{
    public Canvas canvas;
    public MonsterController monster;
    public Image hpmag;
    public Image hpBuffer;
    public GameObject hpBg;

    private float h => monster.GetComponent<CharacterController>().height;
    private void Start()
    {
        hpBg.SetActive(false);
    }
    private void Update()
    {
        if (monster.isBoss)
            return;
        hpBg.SetActive(monster.Hp < 1 && monster.Hp > 0);
        hpBg.transform.position = monster.transform.position + new Vector3(0, h, 0);
        hpBg.transform.rotation = Camera.main.transform.rotation;
        if (hpBuffer.fillAmount > hpmag.fillAmount)
        {
            hpBuffer.fillAmount -= 0.5f * Time.deltaTime;
        }
        else hpBuffer.fillAmount = hpmag.fillAmount;
    }

    public void Hp(float v)
    {
        hpmag.fillAmount = v;
        if (v <= 0.3f)
        {
            hpmag.color = Color.red;
        }
        else if (v <= 0.5)
        {
            hpmag.color = Color.yellow;
        }
        else hpmag.color = Color.green;
    }
}
